package toanstt.com;

import java.io.IOException;
import java.util.Random;

import toanstt.gameplay.BitmapFont;
import toanstt.gameplay.StageMaxScore;
import toanstt.sprites.Sprites;

import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PathDashPathEffect.Style;
import android.util.Log;
import android.view.Display;

public class T
{
	static public int DISPLAY_WIDTH;
	static public int DISPLAY_HEIGHT;
	static public int CELL_WIDTH;
	static public int CELL_HEIGHT;
	static public int CELL_RADIUS;
	static public int CELL_QUAD;
	static public int GAME_SCREEN_HEIGHT;
	static public int GAME_SCREEN_WIDTH;
	static public Display display;
	static public int MARGIN_WIDTH;
	static public int MENU_WIDTH;
	static public int BORDER_WIDTH;
	static public int GAMESCREEN_MIN_X;
	static public int GAMESCREEN_MIN_Y;
	static public int GAMESCREEN_MAX_X;
	static public int GAMESCREEN_MAX_Y;
	static public int GAMESCREEN_HALF_X;
	static public int GAMESCREEN_HALF_Y;
	static public int PANEL_MIN_X;
	static public int PANEL_MIN_Y;
	static public int PANEL_MAX_X;
	static public int PANEL_MAX_Y;
	static public int TIMELINE_HEIGHT;
	static public int ROWS=10;
	static public int COLS=11;
	static public int YES_NO_QR=0; //1 yes 2 no- 0 noq
	static public int int_temp=0;
	static public Paint paint;
	static public Paint paint_alpha;
	static public Paint paint_comment;
	static public Random random;
	static public int V_Y = 10;
	static public boolean USE_BOOM = false;
	static public boolean USE_MAGIC = false;
	
	static public Vibrator vibra;
	
	static public Matrix mat;
	static public int GAME_NUM_DIAMOND=5;
	static public int GAME_NUM_DIAMOND_MAX=10;
	static public int LENG_ARRAY = 121;
	//sprites
	public static Sprites SPRITES_EXPLOSION;
	public static Sprites SPRITES_MAGIC;
	public static Sprites SPRITES_CELL_SPRITES;
	//bitmap
	static public Bitmap[] bitmap_diamond;
	static public Bitmap[] bitmap_diamond_x;
	static public Bitmap bitmap_pos;
	static public Bitmap bitmap_neg;
	static public Bitmap bitmap_locked;
	static public Bitmap[] bitmap_linkin;
	static public Bitmap bitmap_frame_panel;
	static public Bitmap bitmap_timeline_anchor;
	static public Bitmap bitmap_loading;
	static public Bitmap bitmap_button_timech;
	static public Bitmap bitmap_button_stagech;
	static public Bitmap bitmap_button_shop;
	static public Bitmap bitmap_button_howto;
	static public Bitmap bitmap_button_exit;
	static public Bitmap bitmap_button_replay;
	static public Bitmap bitmap_button_menu;
	static public Bitmap bitmap_button_notice;
	static public Bitmap bitmap_button_next;
	static public Bitmap bitmap_button_shop_ulockcell;
	static public Bitmap bitmap_button_shop_lucky;
	static public Bitmap bitmap_button_shop_manasize;
	static public Bitmap bitmap_button_shop_boomsize;
	static public Bitmap bitmap_button_shop_magicsize;
	static public Bitmap bitmap_button_yes;
	static public Bitmap bitmap_button_no;
	static public Bitmap bitmap_button_boom;
	static public Bitmap bitmap_button_boom_n;
	static public Bitmap bitmap_button_magic;
	static public Bitmap bitmap_button_magic_n;
	static public Bitmap bitmap_manapool;
	static public Bitmap bitmap_BackGround1;
	static public BitmapFont[] bitmapFont;
	static public BitmapFont[] bitmapFont_large;
	//player
	static public int PLAY_SCORE=0;
	static public int PLAY_GOLD=0;
	static public int PLAY_CURRENTSTAGE=1;
	static public int PLAY_LUCKY=1;
	static public int PLAY_UNLUCKY=0;
	static public int PLAY_BEST_CROCE=345346;
	static public boolean PLAY_ISTAGE;
	static public String PLAY_NAME="TOANSTT";
	static public int PLAY_MANASIZE=30;
	static public int PLAY_NUMCELL_LOCK=0;
	static public int PLAY_BOOM_LEVEL=2;
	static public int PLAY_MAGIC_LEVEL=4;
	static public int PLAY_CURRENT_MANA=20;
	
	static public StageMaxScore stagesmaxscore;
	
	static public int[][] lockedcells;
	static public int numellcaled;
	static public float UNIT = 60;
	
	static public int SCREEN = 0;
	static public boolean onnotice = false;
	static public Notice notice;
	static public Dialog dialog;
	static public Sound2 sound;
	static public Sound sound1;
	static public void Init_loading(DiamondLinkinActivity parrent)
	{
		display = parrent.getWindowManager().getDefaultDisplay();
        DISPLAY_WIDTH = display.getWidth();
        DISPLAY_HEIGHT= display.getHeight();
        sound = new Sound2();
        
        //sound.Load1();
	}
	static public void Init(DiamondLinkinActivity parrent)
	{
		display = parrent.getWindowManager().getDefaultDisplay();
        DISPLAY_WIDTH = display.getWidth();
        DISPLAY_HEIGHT= display.getHeight();
        MENU_WIDTH =  DISPLAY_HEIGHT/2;
        
        
        CELL_HEIGHT =CELL_WIDTH = (int)(DISPLAY_HEIGHT*0.98/10);
        TIMELINE_HEIGHT = (int)(DISPLAY_HEIGHT-10*CELL_HEIGHT);
        
        BORDER_WIDTH = (int)(DISPLAY_HEIGHT*0.02);
        
        GAME_SCREEN_WIDTH = COLS*CELL_WIDTH;
        GAME_SCREEN_HEIGHT = ROWS*CELL_HEIGHT;
        
        MARGIN_WIDTH = 0;
        
        if(DISPLAY_WIDTH>1.5*DISPLAY_HEIGHT)
        {
        	//BORDER_WIDTH = CELL_HEIGHT/4;
        	MARGIN_WIDTH = (int)(DISPLAY_WIDTH -MENU_WIDTH- GAME_SCREEN_WIDTH - 2*BORDER_WIDTH)/2;
        }
        paint = new Paint();
        paint.setTextSize(25);
        paint_comment = new Paint(); 
        paint_alpha = new Paint();
        GAMESCREEN_MIN_Y =0;
        GAMESCREEN_MAX_Y =GAME_SCREEN_HEIGHT;
        GAMESCREEN_MIN_X = BORDER_WIDTH+MARGIN_WIDTH;
        GAMESCREEN_MAX_X =GAMESCREEN_MIN_X+GAME_SCREEN_WIDTH;
        CELL_RADIUS = CELL_HEIGHT/2;
        CELL_QUAD =CELL_RADIUS; 
        PANEL_MIN_X = MARGIN_WIDTH + 2*BORDER_WIDTH + GAME_SCREEN_WIDTH;
        PANEL_MAX_X = PANEL_MIN_X + MENU_WIDTH;
        PANEL_MIN_Y = 0;
        PANEL_MAX_Y = DISPLAY_HEIGHT;
        random = new Random();
        UNIT = 1.0f*DISPLAY_WIDTH/50;
        GAMESCREEN_HALF_X = (GAMESCREEN_MAX_X*2 + GAMESCREEN_MIN_X*3)/5;
        GAMESCREEN_HALF_Y = (GAMESCREEN_MAX_Y*2 + GAMESCREEN_MIN_Y*3)/5;
        PLAY_SCORE =0;
        stagesmaxscore = new StageMaxScore();
        
        vibra = new Vibrator();
        LoadBitmap();
        LoadSprites();
        LoadLockedCells();
        
        
        notice = new Notice((int)(15*T.UNIT), (int)(1.45f*15*T.UNIT));
        dialog = new Dialog();
       // sound.Load2();
        sound1 = new Sound();
	}
	static void LoadLockedCells()
	{
		lockedcells = new int[COLS][];
		for(int i =0;i<COLS;i++)
			lockedcells[i] = new int[ROWS];
		
		int height1;
		int module;
		int i;
		for(i =0;i<COLS;i++)
			for(int j =0;j < ROWS;j++)
			{
				lockedcells[i][0] = 0;
			}
		if(PLAY_NUMCELL_LOCK > 40)
			PLAY_NUMCELL_LOCK= 40;
		if(PLAY_NUMCELL_LOCK < 0)
			PLAY_NUMCELL_LOCK= 0;
		
		if(PLAY_NUMCELL_LOCK > 19)
		{
			height1 = (PLAY_NUMCELL_LOCK-20)/2;
			module = (PLAY_NUMCELL_LOCK-20)%2;
			for(i =10-1;i>=10-height1;i--)
				lockedcells[1][i] =lockedcells[9][i] = 1;
			if(module == 1)
				lockedcells[1][i] = 1;
			for(i =0;i<10;i++)
				lockedcells[0][i] =lockedcells[10][i] = 1;
		}
		else
		{
			height1 = (PLAY_NUMCELL_LOCK)/2;
			module = (PLAY_NUMCELL_LOCK)%2;
			for(i =10-1;i>=10-height1;i--)
				lockedcells[0][i] =lockedcells[10][i] = 1;
			if(module == 1)
				lockedcells[10][i] = 1;
		}
				
	}
	public static void LoadSprites()
	{
		SPRITES_EXPLOSION = new Sprites("sprites/explosion.sprt");
		SPRITES_MAGIC = new Sprites("sprites/magic.sprt");
		SPRITES_CELL_SPRITES = new Sprites("sprites/t3.sprt");
	}
	public static void LoadBitmap()
	{
		bitmap_diamond = new Bitmap[13];
		bitmap_diamond[0] = loadImageFromAsset("d/d1.png");
		bitmap_diamond[1] = loadImageFromAsset("d/d2.png");
		bitmap_diamond[2] = loadImageFromAsset("d/d3.png");
		bitmap_diamond[3] = loadImageFromAsset("d/d4.png");
		bitmap_diamond[4] = loadImageFromAsset("d/d5.png");
		bitmap_diamond[5] = loadImageFromAsset("d/d6.png");
		bitmap_diamond[6] = loadImageFromAsset("d/d7.png");
		bitmap_diamond[7] = loadImageFromAsset("d/d8.png");
		bitmap_diamond[8] = loadImageFromAsset("d/d9.png");
		bitmap_diamond[9] = loadImageFromAsset("d/d10.png");
		bitmap_diamond[10] = loadImageFromAsset("d/d11.png");
		bitmap_diamond[11] = loadImageFromAsset("d/rock1.png");
		bitmap_diamond[12] = loadImageFromAsset("d/rock2.png");
		
		bitmap_diamond_x = new Bitmap[3];
		bitmap_diamond_x[0] = loadImageFromAsset("d/x2.png");
		bitmap_diamond_x[1] = loadImageFromAsset("d/x3.png");
		bitmap_diamond_x[2] = loadImageFromAsset("d/x4.png");
		
		bitmap_pos = loadImageFromAsset("d/pos.png");
		bitmap_neg = loadImageFromAsset("d/neg.png");
		bitmap_linkin = new Bitmap[3];
		bitmap_linkin[0] = loadImageFromAsset("l/l1.png");
		bitmap_linkin[1] = loadImageFromAsset("l/l2.png");
		bitmap_linkin[2] = loadImageFromAsset("l/l3.png");
		bitmap_locked = loadImageFromAsset("d/locked.png");		
		bitmap_frame_panel = loadImageFromAsset("frame/panel.png");
		
		bitmapFont = new BitmapFont[6];
		bitmapFont[0] = new  BitmapFont("bitmapfont/bitmapfont1.spr",true,2*UNIT/60);
		bitmapFont[1] = new  BitmapFont("bitmapfont/bitmapfont2.spr",true,2.4f*UNIT/60);
		bitmapFont[2] = new  BitmapFont("bitmapfont/bitmapfont3.spr",true,2.8f*UNIT/60);
		bitmapFont[3] = new  BitmapFont("bitmapfont/bitmapfont4.spr",true,3.1f*UNIT/60);
		bitmapFont[4] = new  BitmapFont("bitmapfont/bitmapfont5.spr",true,3.5f*UNIT/60);
		bitmapFont[5] = new  BitmapFont("bitmapfont/bitmapfont6.spr",true,1.5f*UNIT/60);
		
		
		bitmapFont_large = new BitmapFont[6];
		bitmapFont_large[0] = new  BitmapFont("bitmapfont/bitmapfont1.spr",true,5*UNIT/60);
		bitmapFont_large[1] = new  BitmapFont("bitmapfont/bitmapfont2.spr",true,5*UNIT/60);
		bitmapFont_large[2] = new  BitmapFont("bitmapfont/bitmapfont3.spr",true,5*UNIT/60);
		bitmapFont_large[3] = new  BitmapFont("bitmapfont/bitmapfont4.spr",true,5*UNIT/60);
		bitmapFont_large[4] = new  BitmapFont("bitmapfont/bitmapfont5.spr",true,5*UNIT/60);
		bitmapFont_large[5] = new  BitmapFont("bitmapfont/bitmapfont6.spr",true,5*UNIT/60);
		
		
		bitmap_timeline_anchor = loadImageFromAsset("stuff/timeline_anchor.png");
		
		
		bitmap_button_timech = loadImageFromAsset("button/timechangle.png");
		bitmap_button_stagech = loadImageFromAsset("button/stagechange.png");
		bitmap_button_shop = loadImageFromAsset("button/shop.png");
		bitmap_button_howto = loadImageFromAsset("button/howtoplay.png");
		bitmap_button_exit = loadImageFromAsset("button/exit.png");

		bitmap_button_replay = loadImageFromAsset("button/replay.png");
		bitmap_button_menu = loadImageFromAsset("button/menu.png");
		bitmap_button_notice = loadImageFromAsset("button/notice.png");
		bitmap_button_next = loadImageFromAsset("button/next.png");
		
		bitmap_button_shop_ulockcell = loadImageFromAsset("button/unlockcell.png");
		bitmap_button_shop_lucky = loadImageFromAsset("button/lcukycell.png");
		bitmap_button_shop_manasize = loadImageFromAsset("button/manasize.png");
		bitmap_button_shop_boomsize = loadImageFromAsset("button/shop_boom.png");
		bitmap_button_shop_magicsize = loadImageFromAsset("button/shop_magic.png");
		
		bitmap_button_yes = loadImageFromAsset("button/yes.png");;
		bitmap_button_no = loadImageFromAsset("button/no.png");
		
		bitmap_button_boom=loadImageFromAsset("button/boom.png");
		bitmap_button_boom_n=loadImageFromAsset("button/boom_n.png");
		bitmap_button_magic=loadImageFromAsset("button/magic.png");
		bitmap_button_magic_n=loadImageFromAsset("button/magic_n.png");
		bitmap_manapool = loadImageFromAsset("button/manapool.png");
		//load background
		bitmap_BackGround1 = loadImageFromAsset("image/background1.png");
	}
	public static Bitmap loadImageFromAsset(String path) 
	{

		AssetManager am = DiamondLinkinActivity.activity.getAssets();
		try {
			return BitmapFactory.decodeStream(am.open(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	static public void saveGame() 
	{
		SharedPreferences settings = DiamondLinkinActivity.activity.getSharedPreferences("toan", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("GOLD",PLAY_GOLD);
		editor.putInt("STAGE",PLAY_CURRENTSTAGE);
		editor.putInt("LUCKY",PLAY_LUCKY);
		editor.putInt("MANA",PLAY_MANASIZE);
		editor.putInt("BEST",PLAY_BEST_CROCE);
		editor.putInt("NUMLOCK",PLAY_NUMCELL_LOCK);
		editor.commit();
		Exit();
	}
	static public void Exit()
	{
		sound.Exit();
		sound1.Stop();
	}
	static public void loadGame() 
	{
		SharedPreferences settings = DiamondLinkinActivity.activity.getSharedPreferences("toan", 0);
		PLAY_GOLD = settings.getInt("GOLD", 0);
		PLAY_CURRENTSTAGE = settings.getInt("STAGE", 1);
		PLAY_LUCKY = settings.getInt("LUCKY", 1);
		PLAY_MANASIZE = settings.getInt("MANA", 50);
		PLAY_BEST_CROCE = settings.getInt("BEST", 500);
		PLAY_NUMCELL_LOCK = settings.getInt("NUMLOCK", 40);
		//PLAY_NUMCELL_ALIVE = 110 -PLAY_NUMCELL_LOCK;
		//PLAY_GOLD = 9999;
		PLAY_MANASIZE = 1000;
		PLAY_GOLD = 999999999;
		PLAY_NUMCELL_LOCK =0;
		PLAY_LUCKY  = 10;
		PLAY_MANASIZE = 100;
		
	}
	static public int GetProbality(int i)
	{
		if(random.nextInt(101) > i)
			return 0;
		if(random.nextInt(101) > i)
			return 1;
		if(random.nextInt(101) > i)
			return 2;
		return 3;
	}
}




